package com.zh.freechat.assemble;


import com.zh.freechat.common.resp.ChatMsgVO;
import com.zh.freechat.domain.chat.model.ChatMessage;
import com.zh.freechat.domain.message.entity.ChatMsgPO;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author ZH
 * @date 11:04 2022/2/24
 */
public final class ChatMsgWrapper {

    public static ChatMsgVO convertVO(ChatMsgPO entity) {
        var vo = new ChatMsgVO();

        vo.setMsgId(entity.getId().toString());
        vo.setSender(entity.getSender());
        vo.setReceiver(entity.getReceiver());
        vo.setContent(entity.getContent());
        vo.setStatus(entity.getStatus());
        vo.setConvId(entity.getConvId());
        vo.setUrlRef(entity.getUrlRef());
        vo.setType(entity.getType());
        vo.setIsGroup(entity.getIsGroup());

        var formatTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(entity.getCreateTime());
        vo.setCreateTime(formatTime);
        return vo;
    }

    public static ChatMsgPO convertEntity(ChatMessage message) {
        var entity = new ChatMsgPO();

        entity.setId(message.getId());
        entity.setIsGroup(message.getIsGroup());
        entity.setReceiver(message.getReceiver());
        entity.setSender(message.getSender());
        entity.setContent(message.getContent());
        entity.setType(message.getType());
        entity.setUrlRef(message.getUrlRef());
        entity.setChatId(message.getChatId());
        entity.setConvId(message.getConvId());
        //丢弃了时区信息
        var createTime = LocalDateTime.parse(message.getCreateTime(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        entity.setCreateTime(createTime);

        return entity;
    }
}
